package com.nuist.springmvcdemo.Controller;

import com.nuist.springmvcdemo.model.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
// RestController = Response + Controller
public class RequestController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello, SpringMVC!";
    }

    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到参数name:"+name;
    }

    @RequestMapping("/r2")
    public String r2(String name,String Password){
        return "接收到参数name:"+name+",接收到参数Password:"+Password;
    }
    @RequestMapping("/r3")
    public String r3(Person person){
        return person.toString();
    }

    @RequestMapping("/r4")
    public String r4(@RequestParam(value = "time",required = false) String createtime){
        return "接收到参数CreateTime:"+createtime;
    }

    @RequestMapping("/r5")
    public String r5(String[] array){
        return "接收到参数, array" + Arrays.toString(array);
    }

    // 使用集合进行传递
    @RequestMapping("/r6")
    public String r6(@RequestParam List<String> listParam){
        return "size"+ listParam.size()+",listParam:"+listParam;
    }

    // 使用Json进行传递
    @RequestMapping("/r7")
    public String r7(@RequestBody Person person){
        return person.toString();
    }

    // 从URL中获取参数
    @RequestMapping("/r8/{id}/{name}")
    public String r8(@PathVariable("id") Integer sd, @PathVariable("name") String stdname){
        return "id:"+sd+",name:"+stdname;
        // 这里注解跟的参数 还是有具有重命名的作用的
    }

    // 上传文件
    @RequestMapping("/r9")
    public String r9(@RequestPart("file") MultipartFile imgFile){
        String fileName = imgFile.getOriginalFilename();
        return "接受到文件, 文件名为: "+ fileName;
    }
}
